#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>

#define STAT_SOL 0
#define STAT_AIR 1

typedef struct sprite Sprite;

struct sprite
{
	float x,y;
	float vx,vy;
	int status;
};

// liste des prototype des fonction!
int Evolue(Sprite* Sp, Uint8* keys, int *i, SDL_Rect *posperso, SDL_Rect *posFond);
void Saute(Sprite* Sp,float impulsion);
void ControleSol(Sprite* Sp);
void Gravite(Sprite* Sp,float factgravite,float factsautmaintenu,Uint8* keys);
void Collision(SDL_Rect S,SDL_Surface *object,SDL_Rect Rect_Object,int *Active);
int menu();
//-----------------------------------------------------------------------------------------


int main(){
//	int e=0;
//do{
//	e=menu();
//	if(e==1){
//	int f=0;
		
	SDL_Surface *ecrant=NULL, *col=NULL, *background=NULL, *personnage[8]={NULL}, *personnageLeft[8]={NULL}, *imageBossL[3]={NULL}, *imageBossR[3]={NULL};
	SDL_Surface *life=NULL;

	SDL_Rect posLife= {20,20};
	SDL_Rect pos_bg = {0,0};
	SDL_Rect pos_Perso = {100,172};
	SDL_Rect pos_Boss = {1200,575};

	SDL_Rect pos_col = {500,580};
	SDL_Rect pos_col2 = {1200,580}; 

	Uint8 * keys;
	Uint32 timer,elapsed;
	Sprite S;
	int b,numkeys, i=0,j=0,c=0,dBoss=1, bossActive = 1, colActive=1, col2Active=1;
	int Direction=1;
	SDL_Init(SDL_INIT_VIDEO);

	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);
	Mix_Volume(j,MIX_MAX_VOLUME);

	Mix_Chunk *song;
	song=Mix_LoadWAV("1.ogg"); 
	//Mix_PlayMusic(palySong, -1);
	Mix_PlayChannel(1,song,0);

	col= SDL_LoadBMP("image/colusion.bmp");
	life = SDL_LoadBMP("image/coeur.bmp");

	imageBossL[0]= SDL_LoadBMP("image/1/1L.bmp");
	imageBossL[1]= SDL_LoadBMP("image/1/2L.bmp");
	imageBossL[2]= SDL_LoadBMP("image/1/3L.bmp");
	imageBossL[3]= SDL_LoadBMP("image/1/4L.bmp");

	imageBossR[0]= SDL_LoadBMP("image/1/1/1R.bmp");
	imageBossR[1]= SDL_LoadBMP("image/1/1/2R.bmp");
	imageBossR[2]= SDL_LoadBMP("image/1/1/3R.bmp");
	imageBossR[3]= SDL_LoadBMP("image/1/1/4R.bmp");

	personnageLeft[0]= SDL_LoadBMP("image/titus/left/r5.bmp");
	personnageLeft[1]= SDL_LoadBMP("image/titus/left/r4.bmp");
	personnageLeft[2]= SDL_LoadBMP("image/titus/left/r3.bmp");
	personnageLeft[3]= SDL_LoadBMP("image/titus/left/r1.bmp");
	personnageLeft[4]= SDL_LoadBMP("image/titus/left/l5.bmp");
	personnageLeft[5]= SDL_LoadBMP("image/titus/left/l4.bmp");
	personnageLeft[6]= SDL_LoadBMP("image/titus/left/l3.bmp");
	personnageLeft[7]= SDL_LoadBMP("image/titus/left/l2.bmp");
	personnageLeft[8]= SDL_LoadBMP("image/titus/left/l1.bmp");

	personnage[0] = SDL_LoadBMP("image/titus/r5.bmp");
	personnage[1] = SDL_LoadBMP("image/titus/r4.bmp"); 
	personnage[2] = SDL_LoadBMP("image/titus/r3.bmp"); 
	personnage[3] = SDL_LoadBMP("image/titus/r1.bmp"); 
	personnage[4] = SDL_LoadBMP("image/titus/l5.bmp");
	personnage[5] = SDL_LoadBMP("image/titus/l4.bmp"); 
	personnage[6] = SDL_LoadBMP("image/titus/l3.bmp"); 
	personnage[7] = SDL_LoadBMP("image/titus/l2.bmp"); 
	personnage[8] = SDL_LoadBMP("image/titus/l1.bmp"); 

	for(b=0; b<=8; b++){
		SDL_SetColorKey(personnage[b], SDL_SRCCOLORKEY, SDL_MapRGB(personnage[b]->format, 37,176, 000));
		SDL_SetColorKey(personnageLeft[b], SDL_SRCCOLORKEY, SDL_MapRGB(personnageLeft[b]->format, 37,176, 000));
		if(b<4){
			SDL_SetColorKey(imageBossR[b], SDL_SRCCOLORKEY, SDL_MapRGB(imageBossR[b]->format, 37,176, 000));
			SDL_SetColorKey(imageBossL[b], SDL_SRCCOLORKEY, SDL_MapRGB(imageBossL[b]->format, 37,176, 000));
		}
		if(b<1){
			SDL_SetColorKey(col, SDL_SRCCOLORKEY, SDL_MapRGB(col->format, 37,176, 000));
			SDL_SetColorKey(life, SDL_SRCCOLORKEY, SDL_MapRGB(life->format, 255,255,255));
		}
	}

	ecrant = SDL_SetVideoMode(1366,768,32,SDL_FULLSCREEN);
	background = SDL_LoadBMP("image/background/stage2.bmp");
	// ne pas affiché la sourie!
	SDL_ShowCursor(SDL_DISABLE);
	j=0;
	b=0;
	c=0;
	do{
		timer = SDL_GetTicks();
		SDL_FillRect(ecrant,NULL,0);
		SDL_PumpEvents();
		keys = SDL_GetKeyState(&numkeys);
		Direction = Evolue(&S, keys, &i, &pos_Perso, &pos_bg);
			
		if(S.x < 0){
			S.x=0;
			S.y=510;
			i=8;
		}

		if(S.x > 1366-200){
			S.x=1300-200;
			S.y=370;
			i=8;
			pos_bg.x = S.x+2;
		}else{
			pos_bg.x = pos_bg.x - (S.x*7);
		}

		pos_col.x = pos_bg.x+520;
		pos_col2.x = pos_bg.x+1200;
		
		if(pos_Boss.x== (pos_bg.x + 600) ){
			dBoss=1;
		}
		if(pos_Boss.x== (pos_bg.x + 1150) ){
			dBoss=0;
		}

		pos_Boss.x = pos_bg.x+500+c;
		
		if(dBoss==1){
			c++;
		}else{
			c--;
		}

		pos_Perso.x = S.x;
		pos_Perso.y = S.y;
		
		SDL_BlitSurface(background,NULL,ecrant,&pos_bg);
		
		for(b=0;b<3;b++){
			SDL_BlitSurface(life,NULL,ecrant,&posLife);
			posLife.x = posLife.x + 20;
		}
		posLife.x = 20;
		
		SDL_BlitSurface(col,NULL,ecrant,&pos_col);
		SDL_BlitSurface(col,NULL,ecrant,&pos_col2);
		
		if(bossActive == 1){
			for(b=0;b<4;b++){
				//Collision(pos_Perso, imageBossL[i], pos_Boss ,&bossActive);
			}
			j++;
			if(j>2){
				j=0;
			}
			
			if(dBoss==1){
				SDL_BlitSurface(imageBossR[j],NULL,ecrant,&pos_Boss);
				b++;
			}else{
				SDL_BlitSurface(imageBossL[j],NULL,ecrant,&pos_Boss);
				b++;
			}
		}

		if(Direction==1){
			SDL_BlitSurface(personnage[i],NULL,ecrant,&pos_Perso);			
		}else{
			SDL_BlitSurface(personnageLeft[i],NULL,ecrant,&pos_Perso);
		}
		SDL_Flip(ecrant);
	}while (!keys[SDLK_ESCAPE]);
//	}
//}while(e==5);
return 0;
}

int Evolue(Sprite* Sp,Uint8* keys,int *i, SDL_Rect *posperso,SDL_Rect *posFond){
	int Direction = 1;
	float lateralspeed = 1.5f;
	float airlateralspeedfacteur = 0.5f;
	float maxhspeed = 0.2f;
	float adherance = 1.5f;
	float impulsion = 8.0f;
	float factgravite = 0.3f;
	float factsautmaintenu = 3.0f;
	
	// controle lateral
	if (Sp->status == STAT_AIR) // (*2)
		lateralspeed*= airlateralspeedfacteur;
	
	if (keys[SDLK_LEFT]){ // (*1)
		Sp->vx-=lateralspeed;
		*i=(*i+1)%8;
		Direction = 0;
	}
	
	if (keys[SDLK_RIGHT]){
		Sp->vx+=lateralspeed;
		*i=(*i+1)%8;
		Direction = 1;
	}
		
	if (Sp->status == STAT_SOL && !keys[SDLK_LEFT] && !keys[SDLK_RIGHT]){
		Sp->vx/=adherance;
	} // (*3)
	
	// limite vitesse
	if (Sp->vx>maxhspeed) // (*4)
		Sp->vx = maxhspeed;
	
	if (Sp->vx<-maxhspeed)
		Sp->vx = -maxhspeed;
	
	// saut
	if (keys[SDLK_SPACE] && Sp->status == STAT_SOL)
		Saute(Sp,impulsion);
	
	Gravite(Sp,factgravite,factsautmaintenu,keys);
	ControleSol(Sp);

	// application du vecteur à la position.
	Sp->x += Sp->vx;
	Sp->y += Sp->vy;
	return Direction;
}

void Saute(Sprite* Sp,float impulsion){
	Sp->vy = -impulsion;
	Sp->status = STAT_AIR;
}

void ControleSol(Sprite* Sp){
	if (Sp->y>510.0f){
		Sp->y = 510.0f;
		if (Sp->vy>0)
			Sp->vy = 0.0f;
		Sp->status = STAT_SOL;
	}
}

void Gravite(Sprite* Sp,float factgravite,float factsautmaintenu,Uint8* keys){
	if (Sp->status == STAT_AIR && keys[SDLK_SPACE])
		factgravite/=factsautmaintenu;
	Sp->vy += factgravite;
}

void Collision(SDL_Rect S,SDL_Surface *object,SDL_Rect Rect_Object,int *Active){
	if ( (S.x > (Rect_Object.x - 144 )) && (S.x < (Rect_Object.x +object->w)) ){
		if (( (S.y < Rect_Object.y + object->h ) ) && (S.y > (Rect_Object.y - 144) )){
			*Active = 0 ;
		}
	}
}
/*
int menu(){
	int j=0, i=0, c=1;
	int continuer=1;
	SDL_Surface *ecrant=NULL, *background=NULL, *click[7];
	SDL_Rect pos_bg = {0,0};
	SDL_Event event;
	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);
	Mix_Volume(j,MIX_MAX_VOLUME);

	Mix_Chunk *song;
	song=Mix_LoadWAV("1.ogg"); 

	SDL_Rect posClic1= {100,300};
	SDL_Rect posClic2= {100,300+72+10};
	SDL_Rect posClic3= {100,300+72+10+72+10};
	SDL_Rect posClic4= {100,300+72+10+72+10+72+10};

	click[0]= SDL_LoadBMP("image/continues.bmp");
	click[1]= SDL_LoadBMP("image/newGame.bmp");
	click[2]= SDL_LoadBMP("image/setting.bmp");
	click[3]= SDL_LoadBMP("image/exit.bmp");

	click[4]= SDL_LoadBMP("image/continues1.bmp");
	click[5]= SDL_LoadBMP("image/newGame1.bmp");
	click[6]= SDL_LoadBMP("image/setting1.bmp");
	click[7]= SDL_LoadBMP("image/exit1.bmp"); 

	for(i=0;i<8;i++){
		SDL_SetColorKey(click[i], SDL_SRCCOLORKEY, SDL_MapRGB(click[i]->format, 37,176, 000));
	}
	
	//Mix_PlayMusic(palySong, -1);
	//--------------------------------
	Uint8 * keys;
	Uint32 timer,elapsed;
	int numkeys;
	int Direction=1;
	SDL_Init(SDL_INIT_VIDEO);
	Mix_PlayChannel(1,song,0);
	ecrant = SDL_SetVideoMode(1366,768,32,SDL_FULLSCREEN);
	background = SDL_LoadBMP("image/background/menuBg.bmp");
	SDL_BlitSurface(background,NULL,ecrant,&pos_bg);
	while(continuer==1 ){
		SDL_WaitEvent(&event);
		if(c==3){
			continuer=0;
		}
		if(event.type==SDL_QUIT){ 
			continuer = 0; 
		}else if(event.type==SDL_KEYDOWN){
			switch(event.key.keysym.sym){
				case SDLK_UP: // Flèche haut
					if(c>0){
						c--;
					}
					break;
				case SDLK_DOWN:
					if(c<3){
						c++;
					}
					break;
				case SDLK_ESCAPE:
					continuer = 0;
					break;
			}	
		
			if(c==0){
				SDL_BlitSurface(click[4],NULL,ecrant,&posClic1);
			}else{
				SDL_BlitSurface(click[0],NULL,ecrant,&posClic1);
			}
			
			if(c==1){
				SDL_BlitSurface(click[5],NULL,ecrant,&posClic2);
			}else{
				SDL_BlitSurface(click[1],NULL,ecrant,&posClic2);
			}
			
			if(c==2){
				SDL_BlitSurface(click[6],NULL,ecrant,&posClic3);
			}else{
				SDL_BlitSurface(click[2],NULL,ecrant,&posClic3);
			}
			
			if(c==3){
				SDL_BlitSurface(click[7],NULL,ecrant,&posClic4);
			}else{
				SDL_BlitSurface(click[3],NULL,ecrant,&posClic4);
			}
			SDL_Flip(ecrant);
			//------------------------------------------------------------------------------------
		}
	}
	return c;
}*/